/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * listaPagosPendientesBanco.java
 *
 * Created on Jan 7, 2012, 9:29:26 AM
 */

package INTERFAZ;

import CONTROLES.ControlPago;
import CONTROLES.ControlSucursal;
import CONTROLES.ControlTipoCambio;
import MODELOS.Cliente;
import MODELOS.Cuenta;
import MODELOS.ListaPagos;
import MODELOS.Pago;
import MODELOS.Sucursal;
import MODELOS.TipoCambio;
import UTILITARIOS.Fecha;
import UTILITARIOS.Herramienta;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import UTILITARIOS.MiModelo;
import UTILITARIOS.StringToDate;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author VICENTE
 */
public class listaPagosPorRezagar extends javax.swing.JDialog {
    /** Creates new form listaPagosPendientesBanco */
    public listaPagosPorRezagar(javax.swing.JFrame parent, boolean modal){
        super(parent, modal);
        initComponents();
        iniciarComponentes();
        addEventos();
    }

    private void iniciarComponentes(){

        controlPago = new ControlPago();
        fechaI = new Fecha();
        tipoCambio = new ControlTipoCambio().dameCambio();        
        sucursal = Bienvenida.sucursal;
        
        jLabel11.setText("Sucursal - "+sucursal.getNombreSuc());
        jLabel3.setText("AL "+fechaI.getFechaPrincipal());

        String sucursales[] = new ControlSucursal().nombresSuc();        
        for(int i = 0; i<sucursales.length; i++)
           choice1.add(sucursales[i]);

        choice1.select(sucursal.getIdSucursal()+"-"+sucursal.getNombreSuc());
       
        if(Bienvenida.user.getTipoUsuario().equalsIgnoreCase("ADMINISTRADOR") && Bienvenida.user.getToken().equalsIgnoreCase("INGRESAR") ){
           choice1.setEnabled(true);
           }else{
           choice1.setEnabled(false);
           }

        listPagos = controlPago.getPagosPorRezagar(sucursal.getIdSucursal(), fechaI.getFechaDate());

        cargarTabla();
    }
    
    private void addEventos(){

        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                StringTokenizer s = new StringTokenizer(choice1.getSelectedItem(),"-");
                int idSucursal = Integer.parseInt(s.nextToken());                               

                listPagos = controlPago.getPagosPorRezagar(idSucursal, fechaI.getFechaDate());
                cargarTabla();
            }
        });
        jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //rezagar pagos
                if(controlPago.rezagarPagos(listPagos))
                    JOptionPane.showMessageDialog(null, "Se Rezagaron todos los pagos de la lista Correctamente", "Confirmación", JOptionPane.ERROR_MESSAGE);
                else
                    JOptionPane.showMessageDialog(null, "Ocurrio algun error al REZAGAR algun pago de la lista", "Confirmación", JOptionPane.ERROR_MESSAGE);
                dispose();
            }
        });

        salir.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        pagos = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        salir = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        choice1 = new java.awt.Choice();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lista de pagos para ser rezagados");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "LISTA DE PAGOS VENCIDOS", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 14))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        pagos.setName("pagos"); // NOI18N
        jScrollPane1.setViewportView(pagos);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1239, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 12));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("LISTA DE  PAGOS PENDIENTES QUE PUEDEN SER REZAGADOS");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("DEL :     AL:");
        jLabel3.setName("jLabel3"); // NOI18N

        salir.setFont(new java.awt.Font("Tahoma", 0, 12));
        salir.setText("SALIR");
        salir.setName("salir"); // NOI18N

        jLabel11.setFont(new java.awt.Font("Times New Roman", 0, 12));
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel11.setText("jLabel11");
        jLabel11.setName("jLabel11"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(salir))
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 1261, Short.MAX_VALUE)
            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1261, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 1251, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addGap(16, 16, 16)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(salir)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de busqueda"));
        jPanel3.setName("jPanel3"); // NOI18N

        choice1.setName("choice1"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 12));
        jLabel2.setText("Elegir Sucursal");
        jLabel2.setName("jLabel2"); // NOI18N

        jButton1.setText("Listar");
        jButton1.setName("jButton1"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(106, 106, 106))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1))
        );

        jButton2.setText("RezagarTodos los Pagos");
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(412, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(191, 191, 191)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Choice choice1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable pagos;
    private javax.swing.JButton salir;
    // End of variables declaration//GEN-END:variables
    private MiModelo modelo_pagos;

    ArrayList<ListaPagos> listPagos;
    private TipoCambio tipoCambio;

    private ControlPago controlPago;
    Fecha fechaI;
    private Sucursal sucursal;

    private void cargarTabla() {
        modelo_pagos = new MiModelo(new Object [][] {},new String [] {"N# CUENTA NFN", "Plan", "Nombre", "Telefono", "Celular", "N# pago", "fecha Pago","Estado", "Monto Sus", "Monto Bs"}, -1);
        ListaPagos l;
        Cuenta cu;
        Cliente cli;
        Pago p;
        StringToDate st = new StringToDate();
        if(listPagos.size()>0){
            for(int i=0;i<listPagos.size();i++){
                l = listPagos.get(i);
                p = l.getPago();
                cu = l.getCuenta();
                cli = l.getCliente();
                Object [] auxPago = {
                    cu.getNumeroCuenta(),
                    l.getPlan().getNombrePlan(),
                    cli.getNombreCompletoCliente(),
                    cli.getTelefono(),
                    cli.getCelular(),
                    p.getNumeroPago(),
                    st.dateAstring(p.getFechaPago()),
                    p.getEstado(),
                    Herramienta.redondearDoubleString(p.getMonto()),
                    Herramienta.redondearDoubleString(p.getMonto()*tipoCambio.getTipoCompra())
                };
                modelo_pagos.addRow(auxPago);
            }
        }
        pagos.setModel(modelo_pagos);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.RIGHT);

        DefaultTableCellRenderer tcrc = new DefaultTableCellRenderer();
        tcrc.setHorizontalAlignment(SwingConstants.CENTER);

        pagos.getColumnModel().getColumn(0).setMaxWidth(120);
        pagos.getColumnModel().getColumn(0).setMinWidth(100);
        pagos.getColumnModel().getColumn(0).setCellRenderer(tcr);
        pagos.getColumnModel().getColumn(1).setMaxWidth(110);
        pagos.getColumnModel().getColumn(1).setMinWidth(110);
        pagos.getColumnModel().getColumn(1).setCellRenderer(tcrc);
        pagos.getColumnModel().getColumn(2).setCellRenderer(tcrc);
        pagos.getColumnModel().getColumn(3).setMaxWidth(80);
        pagos.getColumnModel().getColumn(3).setCellRenderer(tcrc);
        pagos.getColumnModel().getColumn(4).setMaxWidth(80);
        pagos.getColumnModel().getColumn(4).setCellRenderer(tcrc);
        pagos.getColumnModel().getColumn(5).setMaxWidth(80);
        pagos.getColumnModel().getColumn(5).setCellRenderer(tcrc);
        pagos.getColumnModel().getColumn(6).setMaxWidth(110);
        pagos.getColumnModel().getColumn(6).setMinWidth(110);
        pagos.getColumnModel().getColumn(6).setCellRenderer(tcrc);
        pagos.getColumnModel().getColumn(7).setMaxWidth(110);
        pagos.getColumnModel().getColumn(7).setMinWidth(110);
        pagos.getColumnModel().getColumn(7).setCellRenderer(tcrc);
        pagos.getColumnModel().getColumn(8).setMaxWidth(80);
        pagos.getColumnModel().getColumn(8).setCellRenderer(tcr);
        pagos.getColumnModel().getColumn(9).setMaxWidth(80);
        pagos.getColumnModel().getColumn(9).setCellRenderer(tcr);
    }
}
